package com.ie.controller.before;

import com.ie.common.http.ResponseResult;
import com.ie.common.security.anno.AuthIgrone;
import com.ie.entity.admin.CartEntity;
import com.ie.service.admin.GoodsService;
import com.ie.service.before.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/before/cart")
public class CartController<cartEntity> {

    @Autowired
    private CartService cartService;

    @Autowired
    private GoodsService goodsService;


    // 前端请求的 /api/before/cart/myCart 接口
    @AuthIgrone
    @PostMapping("/myCart")
    public ResponseResult<Map<String, Object>> myCart(@RequestBody Map<String, String> requestMap) {
        String busertableId = requestMap.get("busertableId");
        if (busertableId == null) {
            return ResponseResult.error("用户 ID 不能为空");
        }
        return cartService.getCartListByBusertableId(busertableId);
    }


    // 前端请求的 /api/before/cart/removeCart 接口
    @AuthIgrone
    @PostMapping("/removeCart")
    public ResponseResult<Map<String, Object>> removeCart(@RequestBody Map<String, Integer> requestMap) {
        Integer id = requestMap.get("id");
        if (id == null) {
            return ResponseResult.error("购物车 ID 不能为空");
        }
        return cartService.removeCart(id);
    }

    @AuthIgrone
    @PostMapping("/bupDateCart")
    public ResponseResult<Map<String, Object>> bupDateCart(@RequestBody CartEntity cartEntity) {

        return cartService.bupDateCart(cartEntity);
    }

    @AuthIgrone
    @PostMapping("/clearCart")
    public ResponseResult<Map<String, Object>> clearCart(@RequestBody CartEntity cartEntity) {
        return cartService.clearCart(cartEntity);
    }

    // 获取用户购物车列表


    // 【CartController接口】获取购物车列表接口，接收包含userId的Map参数
    @AuthIgrone
    @PostMapping("/list")
    public ResponseResult<Map<String, Object>> getCartList(@RequestBody Map<String, Integer> requestMap) {
        Integer userId = requestMap.get("userId");
        if (userId == null) {
            return ResponseResult.error("用户 ID 不能为空");
        }
        return cartService.getCartList(userId);
    }

    @AuthIgrone
    @PostMapping("/add")
    public ResponseResult<Map<String, Object>> addCart(@RequestBody CartEntity cartEntity) {
        return cartService.addCart(cartEntity);
    }
}